layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 成本标准配置表管理
     */
    var BasicCostStandardConf = {
        tableId: "basicCostStandardConfTable"
    };

    /**
     * 初始化表格的列
     */
    BasicCostStandardConf.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'vehLimitTyp', sort: true, title: '车限类型'},
            {field: 'vehLimitMin', sort: true, title: '车限最小值'},
            {field: 'vehLimitMax', sort: true, title: '车限最大值'},
            {field: 'kiloRangeMin', sort: true, title: '公里最小值'},
            {field: 'kiloRangeMax', sort: true, title: '公里最大值'},
            {field: 'price', sort: true, title: '单价'},
            {field: 'odr', sort: true, title: '序号'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicCostStandardConf.search = function () {
        var queryData = {};
        queryData['proId'] = $("#operateId").val();
        queryData['vehLimitTyp'] = $("#q_veh_limit_typ").val();
        queryData['qVehLimit'] = $("#q_veh_limit").val();
        queryData['qKilo'] = $("#q_kilo").val();
        table.reload(BasicCostStandardConf.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 // $('#btnSearch').click(function () {
                 //     BasicCostStandardConf.search();
                 // });
                 // // 添加按钮点击事件
                 // $('#btnAdd').click(function () {
                 //     BasicCostStandardConf.openAddDlg();
                 // });
                 // // 导出excel
                 // $('#btnExp').click(function () {
                 //     BasicCostStandardConf.exportExcel();
                 // });
                 // // 导入
                 // $('#btnExpimport').click(function () {
                 //     layer.open({
                 //         type: 2,
                 //         title: '成本标准导入',
                 //         area: ['1000px', '750px'],
                 //         anim: 5,
                 //         content: Feng.ctxPath + '/basicCostStandardConf/toimportExcel',
                 //         end: function () {
                 //             table.reload(BasicCostStandardConf.tableId);
                 //         }
                 //     });
                 // });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    BasicCostStandardConf.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicCostStandardConf/add';
    };

    /**
     * 导出excel按钮
     */
    BasicCostStandardConf.exportExcel = function () {
        var checkRows = table.checkStatus(BasicCostStandardConf.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['proId'] = $("#operateId").val();
            queryData['vehLimitTyp'] = $("#q_veh_limit_typ").val();
            queryData['qVehLimit'] = $("#q_veh_limit").val();
            queryData['qKilo'] = $("#q_kilo").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/basicCostStandardConf/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });

        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicCostStandardConf.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicCostStandardConf/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicCostStandardConf.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicCostStandardConf/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BasicCostStandardConf.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicCostStandardConf.tableId,
        url: Feng.ctxPath + '/basicCostStandardConf/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicCostStandardConf.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicCostStandardConf.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicCostStandardConf.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicCostStandardConf.exportExcel();
    });

    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '成本标准导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicCostStandardConf/toimportExcel',
            end: function () {
                table.reload(BasicCostStandardConf.tableId);
            }
        });
    });

    // 工具条点击事件
    table.on('tool(' + BasicCostStandardConf.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicCostStandardConf.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicCostStandardConf.onDeleteItem(data);
        }
    });
});
